package com.example.demo_0828.service.impl;

import com.example.demo_0828.pojo.dto.AssignDTO;
import com.example.demo_0828.pojo.dto.PageQueryDTO;
import com.example.demo_0828.pojo.dto.WorkOrderDTO;
import com.example.demo_0828.pojo.entity.WorkOrder;
import com.example.demo_0828.pojo.mapper.WorkOrderMapper;
import com.example.demo_0828.pojo.result.PageResult;
import com.example.demo_0828.pojo.vo.WorkOrderVO;
import com.example.demo_0828.service.WorkOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private AssignDTO assignDTO;

    /**
     * 添加
     *
     * @param workOrderDTO
     */
    @Override
    public void save(WorkOrderDTO workOrderDTO) {
        WorkOrder workOrder = new WorkOrder();
        BeanUtils.copyProperties(workOrderDTO, workOrder);
        workOrderMapper.insert(workOrder);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        workOrderMapper.deleteByPrimaryKey(ids);
    }

    /**
     * 修改
     *
     * @param workOrderDTO
     */
    @Override
    public void update(WorkOrderDTO workOrderDTO) {
        WorkOrder workOrder = new WorkOrder();
        BeanUtils.copyProperties(workOrderDTO, workOrder);
        workOrderMapper.update(workOrder);
    }

    /**
     * 分页查询
     *
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PageQueryDTO pageQueryDTO) {
//        分页设置
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<WorkOrderVO> page = workOrderMapper.pageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 分派
     *
     * @param assignDTO
     */
    @Override
    public void assign(AssignDTO assignDTO) {
        WorkOrder order = workOrderMapper.selectByPrimaryKey(assignDTO.getId());
        if (assignDTO.getId() == null && order == null) {
            System.out.println("ID无效");
        }
        assignDTO.setFenpaiTime(LocalDateTime.now());
        workOrderMapper.assign(assignDTO);

    }

    /**
     * 查询工单总量和超期率1
     * @param month
     */
    @Override
    public void select1(Integer month) {
        workOrderMapper.select1();
    }

    /**
     * 查询工单总量和超期率2
     * @param month
     */
    @Override
    public void select2(Integer month) {
        workOrderMapper.select2();
    }
}
